<?php

declare(ENCODING = 'UTF-8');

/*                                                                        *
 * This script is part of the TYPO3 project - inspiring people to share!  *
 *                                                                        *
 * TYPO3 is free software; you can redistribute it and/or modify it under *
 * the terms of the GNU General Public License version 2 as published by  *
 * the Free Software Foundation.                                          *
 *                                                                        *
 * This script is distributed in the hope that it will be useful, but     *
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *	
 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General      *
 * Public License for more details.                                       *
 *                                                                        */	

/**
 * @package auxo
 * @subpackage Logging
 * @version $Id$
 */

/**	
 * The Log File Appender
 *
 * This log appender simply appends log entries to a given output file.
 * 
 * @package auxo
 * @subpackage Logging
 * @version $Id$	
 * @copyright Copyright belongs to the respective authors
 * @author AHN
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License, version 2
 */
class  tx_auxo_LogFileAppender implements tx_auxo_LogAppenderInterface {
	/**
	 * A File handle
	 *
	 * @var $handle
	 */
	private $handle = NULL;
	
	/**
	 * A Log Filename
	 *
	 * @var string
	 */
	private $filename = '';
	
	/**
	 * A Log Layout
	 *
	 * @var tx_auxo_LogLayoutInterface
	 */
	private $layout = NULL;

	/**
	 * Creates a new Log file Appender with a given output filename
	 *
	 * @param string $filename
	 */
	public function __construct($filename) {
		$this->openFile($filename);		
		$this->filename = $filename;
	}
	
	/**
	 * Defines a layout for this appender
	 *
	 * @param tx_auxo_LogLayoutInterface $layout
	 * @return void
	 */
	public function setLayout(tx_auxo_LogLayoutInterface $layout) {
		$this->layout = $layout;
	}
	
	/**
	 * Appends an event to this log appender
	 *
	 * @param tx_auxo_logEvent $event a log event 
	 * @return void
	 */	
	public function append(tx_auxo_LogEvent $event) {
		if ($this->layout) {
			$output = $this->layout->format($event);
		}
		else {
			$output = sprintf('%s', $event);
		}
		fwrite($this->handle, $output);
		fflush($this->handle);
	}
	
	/**
	 * Close this log file
	 *
	 * @return void
	 */
	public function close() {
		if ($this->handle) fclose($this->handle);
	}
	
	/**
	 * Returns the current log filename
	 * 
	 * @return string $filename name of current log file
	 */
	public function getFilename() {
		return $this->filename;	
	}
	
	/**
	 * Opens a existing or creates a new log file
	 *
	 * @param string $filename
	 * @throws tx_auxo_IOException if directory is not writeable
	 */
	protected function openFile($filename) {
		if (! is_writeable(dirname($filename))) {
			throw new tx_auxo_IOException(sprintf('Logfile %s could not be created', $this->filename));
		}
		$this->handle = fopen($filename, 'a');		
	}
}
?>